<template>
  <div class="banner-Container">
    <div class="head">
      <div class="opt">
        <el-button type="primary" @click="addBanner()">新增</el-button>
      </div>
    </div>
    <el-table :data="List" border style="width: 100%">
      <el-table-column prop="index" label="序号" width="120"></el-table-column>
      <el-table-column prop="num" label="顺序" width="120"></el-table-column>
      <el-table-column label="图片url">
        <template slot-scope="scope">
          <img :src="`http://mov.freebd.cn:808/file${scope.row.imgUrl}`" style="height:80px">
        </template>
      </el-table-column>
      <el-table-column prop="title" label="提示文字"></el-table-column>
       <el-table-column prop="hide" label="是否显示">
               <template slot-scope="scope">
              <el-button type="text" v-show="scope.row.hide == 0">隐藏</el-button>
              <el-button type="text" v-show="scope.row.hide == 1">显示</el-button>
            </template>
       </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>&nbsp;&nbsp;
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增轮播图 -->
    <el-dialog title="轮播图" @close="Cancel" :visible.sync="dialogAdd" width="30%">
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="顺序" prop="num">
          <el-input v-model="ruleForm.num" placeholder="请输入顺序"></el-input>
        </el-form-item>

        <el-form-item label="提示文字" prop="title">
          <el-input v-model="ruleForm.title" placeholder="请输入提示文字"></el-input>
        </el-form-item>

        <el-form-item label="banner图片 " prop="imgUrl">
          <el-upload
            list-type="picture-card"
            action="http://mov.freebd.cn:808/img/upload"
            :file-list="fileList1"
            :on-success="onSuccess1"
            :on-remove="onRemove1"
            :limit="1"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="是否隐藏" prop="title">
          <el-select v-model="ruleForm.hide" placeholder="请选择" >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <div style="text-align:right;">
          <el-button @click="dialogAdd = false">取 消</el-button>
          <el-button type="primary" @click.native="addClick('ruleForm')">确 定</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      List: [],
      val: 1,
      total: 0,
      flag: 1,
      fileList1: [],
      dialogAdd: false,
      editId: "",
      ruleForm: {
        sum: "",
        title: "",
        imgUrl: "",
        hide: 1
      },
      rules: {
        imgUrl: [
          { required: true, message: "请上传banner图片", trigger: "blur" }
        ]
      },
      options: [
        {
          value: 1,
          label: "显示"
        },
        {
          value: 0,
          label: "隐藏"
        }
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    onSuccess1(Response, file, fileList) {
      this.ruleForm.imgUrl = file.response.data.path;
    },
    onRemove1(file, fileList) {
      this.ruleForm.businessLicence = "";
      this.fileList1 = [];
    },
    Cancel() {
      this.fileList1 = [];
      this.dialogAdd = false;
    },
    //编辑
    handleEdit(row) {
      this.editId = row.id;
      this.dialogAdd = true;
      this.flag = 2;
      this.ruleForm.num = row.num;
      this.ruleForm.title = row.title;
      this.ruleForm.imgUrl = row.imgUrl;
      this.ruleForm.hide = row.hide;
      if (this.ruleForm.imgUrl) {
        this.fileList1.push({
          url: "http://mov.freebd.cn:808/file" + this.ruleForm.imgUrl
        });
      } else {
        this.fileList1 = [];
      }
    },
    //新增
    addBanner() {
      this.dialogAdd = true;
      this.flag = 1;
    },
    //确定
    addClick(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.flag == 1) {
            let url = "/banner/add";
            let params = {
              num: this.ruleForm.num,
              title: this.ruleForm.title,
              imgUrl: this.ruleForm.imgUrl,
              hide:this.ruleForm.hide,
            };
            this.httpPost(url, params, res => {
              if (res.code == 200) {
                this.$message({
                  message: "新增成功",
                  type: "success"
                });
                this.getList();
              } else {
                this.$message({
                  message: res.message,
                  type: "warning"
                });
              }
              this.dialogAdd = false;
              this.ruleForm = {};
            });
          } else {
            let url = "/banner/edit";
            let params = {
              id: this.editId,
              hide: 1,
              num: this.ruleForm.num,
              title: this.ruleForm.title,
              imgUrl: this.ruleForm.imgUrl,
                hide:this.ruleForm.hide,
            };
            this.httpPost(url, params, res => {
              if (res.code == 200) {
                this.$message({
                  message: "编辑成功",
                  type: "success"
                });
                this.getList();
              } else {
                this.$message({
                  message: res.message,
                  type: "warning"
                });
              }
              this.dialogAdd = false;
              this.ruleForm = {};
            });
          }
        }
      });
    },
    //获取列表
    getList() {
      let url = "/banner/findList";
      let params = {};
      this.httpPost(url, params, res => {
        if (res.code == 200) {
          this.List = res.data.list;
          this.total = res.data.total;
          this.List.forEach((element, i) => {
            this.List[i].index = (this.val - 1) * 10 + i + 1;
          });
        }
      });
    },
    //删除
    handleDelete(row) {
      this.$confirm("是否删除此数据？", "删除", {
        distinguishCancelAndClose: true,
        confirmButtonText: "删除",
        cancelButtonText: "取消"
      })
        .then(() => {
          let url = "/banner/edit";
          let params = {
            id: row.id,
            status: 1
          };
          this.httpPost(url, params, res => {
            if (res.code == 200) {
              this.$message({
                message: "删除成功",
                type: "success"
              });
              this.getList();
            } else {
              this.$message({
                type: "warning",
                message: res.message
              });
            }
          });
        })
        .catch(action => {
          this.$message({
            type: "info",
            message: "取消删除"
          });
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.banner-Container {
  .head {
    height: 60px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .search {
      width: 300px;
    }
  }
}
</style>

<style lang="scss">
.banner-Container {
  .el-select{
    width:100%;
  }
    .el-button + .el-button {
    margin-left: 0px !important;
  }
}
</style>